﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;
using ConfigLibrary;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;
using System.Data.Common;

namespace Contacts
{
    class update
    {
        private Config config;
        MySqlCommand contcmd = new MySqlCommand();
        MySqlDataAdapter contda2 = new MySqlDataAdapter();
        DataTable contcomp = new DataTable();
        MySqlConnection conn = new MySqlConnection();
        MySqlCommand compcmd = new MySqlCommand();
        MySqlDataAdapter compda = new MySqlDataAdapter();
        private string _username;
        private string _host;
        private string _pass;
        private string _database;

        public void loadconfig()
        {
            try
            {

                //this switches between the mysql contacts and mysql test databases
//#if (DEBUG)

                Stream s = File.Open(Properties.Settings.Default.CONFIGPATHTEST, FileMode.Open);
                //Stream s = File.Open(Properties.Settings.Default.CONFIGPATH, FileMode.Open);

//#elif (RELEASE)
                 //Stream s = File.Open(Properties.Settings.Default.CONFIGPATH, FileMode.Open);
//#endif

                BinaryFormatter b = new BinaryFormatter();
                config = (Config)b.Deserialize(s);
                s.Close();
                config.GetSqlInfo(out _host, out _username, out _pass, out _database);
            }
            catch (Exception ex)
            {
                mail("Config File", ex);
                MessageBox.Show("Please see Doug.  The Contacts data didn't load correctly.");
                throw;
            }
            //end load
        }
        
        private void mail(string title, Exception ex)
        {
            Mail.mailman.sendmail(string.Format("MySql connection failed to load {0}", title), string.Format("Method call: {0} \r\nError Message: {1} \r\nCall Stack: {2}", ex.TargetSite, ex.Message, ex.StackTrace));
        }

        public void connect()
        {
            try
            {
                conn.ConnectionString = "Database=" + _database + ";Data Source=" + _host + ";User Id=" + _username + ";Password=" + _pass;
                conn.Open();
                GC.KeepAlive(conn);
            }
            catch (Exception ex)
            {
                throw new MySqlException("Open Connection failed", ex);
            }
        }
        
        public void loadcontact(int contact_id, ref DataTable con)
        {
            
            contcmd.CommandText = "Select * from contacts where contact_id =" + contact_id + "";
            contcmd.Connection = conn;
            contda2.SelectCommand = contcmd;            
            contda2.TableMappings.Add("contacts", "contcomp");
            contda2.FillSchema(contcomp, SchemaType.Source);            
            contda2.MissingSchemaAction = MissingSchemaAction.AddWithKey;
            contda2.Fill(contcomp);            
            contcmd.ExecuteNonQuery();
            con = contcomp;
        }

        public void loadcompany(int comp_id, DataTable com)
        {
            compcmd.CommandText = "Select * from companies where company_id =" + comp_id + "";
            compcmd.Connection = conn;
            compda.SelectCommand = compcmd;
            compda.TableMappings.Add("companies", "contcomp");
            compda.FillSchema(contcomp, SchemaType.Source);
            compda.MissingSchemaAction = MissingSchemaAction.AddWithKey;
            compda.Fill(contcomp);
            compcmd.ExecuteNonQuery();
            com = contcomp;
        }
    }
}
